Correlated Subquery


Introduction to Correlated Subqueries in SQL

In the world of databases and SQL, subqueries are like the super detectives that help you find exactly the information you're looking for. One really smart type of subquery is called a "correlated subquery." It's like a query within a query and it's special because it's super flexible and can adapt to the context of what you're looking at. Think of it as a way to ask specific questions about your data based on what you're currently looking at. It's pretty neat because it helps you get targeted results, like a search tailored just for you. Let's dive deeper into to understand this.

What is a Correlated Subquery?

A correlated subquery is a type of subquery that references one or more columns from the outer query. Unlike a non-correlated subquery, which operates independently, a correlated subquery relies on the data from the outer query to execute.

The correlation between the inner and outer queries is established through a reference to a column in the outer query within the inner query's predicates. This enables the subquery to be re-evaluated for each row processed by the outer query, making it contextually adaptable.

Structure of a Correlated Subquery

The basic structure of a correlated subquery involves embedding one query (inner query) within another query (outer query) and establishing a connection between them using a shared column or condition. Here's a general outline:

SELECT column1, column2, ...

FROM table1

WHERE condition_column = (

SELECT aggregate_function(column)

FROM table2

WHERE condition_column = table1.column

);

In this structure:

  • The outer query retrieves data from 'table1'.
  • The inner query is executed for each row in 'table1', utilizing the value from 'table1.column' to filter data from 'table2'.

Example

Consider a scenario where we have two tables: Employees and Salaries. We want to find employees whose salaries are higher than the average salary in their respective departments. Here's how a correlated subquery can accomplish this task:

Employees Table:

Employee_ID

Name

Department

1

Alice

Sales

2

Bob


3

Charlie

Sales

4

David


Salaries Table:

Employee_ID

Salary

1

50000

2

60000

3

48000

4

55000

Using a correlated subquery, the SQL query would look like:

SELECT Name, Department, Salary

FROM Employees e

WHERE Salary > (

SELECT AVG(Salary)

FROM Salaries s

WHERE e.Department = s.Department

);

  • The outer query (SELECT Name, Department, Salary FROM Employees e) retrieves data from the Employees table.
  • The inner query (SELECT AVG(Salary) FROM Salaries s WHERE e.Department = s.Department) calculates the average salary for each department in the Salaries table.
  • The correlation is established by comparing the Department column in both tables (e.Department = s.Department).


Correlated subqueries are a valuable tool in SQL, allowing for intricate data retrieval based on contextual relationships between queries. Understanding their structure and usage enables SQL developers to craft more sophisticated and targeted queries, efficiently extracting specific information from databases.